<?php

namespace App\Models\Appraise;

use App\Models\BaseModel;
use App\Models\Common\ResourcesModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class AppraiseModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $fillable = [
        'id',
        'workOrderNo',
        'shopId',
        'type',
        'score',
        'desc',
        'tag',
        'status',
        'dealUid',
        'dealTime',
        'mobile',
        'tagDesc',
        'isDeleted',
        'createdAt',
        'updatedAt',
        'createdByUid',
        'updatedByUid',
    ];

    protected $table = 'appraise';

    const TYPE_ALL                    = 0;
    const TYPE_WORK_ORDER             = 1;
    const TYPE_APP                    = 2;
    const TYPE_OPENSHOP_SIGN          = 3;
    const TYPE_OPENSHOP_SET_LOCATION  = 4;
    const TYPE_OPENSHOP_PREPARATION   = 5;
    const TYPE_OPENSHOP_COLLEGE       = 6;
    const TYPE_KFOS_FIRST_ORDER       = 7;
    const TYPE_OPENSHOP_OPENING       = 8;
    const TYPE_KFOS_ORDER             = 9;
    const TYPE_WAIMAI_OPENING         = 10;
    const TYPE_GROUP_PURCHASE_OPENING = 11;
    const TYPE_APP_COMPLAINT          = 12;
    const TYPE_APP_CHOOSE_SUGGESTION  = 13;
    const TYPE_APP_PROCESS_IMPROVE    = 14;
    const TYPE_APP_SHOP_DESIGN        = 15;

    const TYPE_MAP_TEXT = [
        self::TYPE_WORK_ORDER             => 'workOrder',
        self::TYPE_APP                    => 'APP',
        self::TYPE_OPENSHOP_SIGN          => 'openShopSign',
        self::TYPE_OPENSHOP_SET_LOCATION  => 'openShopSetLocation',
        self::TYPE_OPENSHOP_PREPARATION   => 'openShopPreparation',
        self::TYPE_OPENSHOP_COLLEGE       => 'openShopCollege',
        self::TYPE_KFOS_FIRST_ORDER       => 'kfosFirstOrder',
        self::TYPE_OPENSHOP_OPENING       => 'openShopShopOpening',
        self::TYPE_KFOS_ORDER             => 'kfosOrder',
        self::TYPE_WAIMAI_OPENING         => 'waimaiOpening',
        self::TYPE_GROUP_PURCHASE_OPENING => 'groupPurchaseOpening',
        self::TYPE_APP_COMPLAINT          => 'APPComplaint',
        self::TYPE_APP_PROCESS_IMPROVE    => 'APPProcessImprove',
        self::TYPE_APP_SHOP_DESIGN        => 'APPShopDesign',
    ];


    const TYPE_CN_MAP_TEXT = [
        self::TYPE_WORK_ORDER             => '工单-工单评价',
        self::TYPE_APP                    => '商家APP-反馈',
        self::TYPE_OPENSHOP_SIGN          => '开店-签约阶段',
        self::TYPE_OPENSHOP_SET_LOCATION  => '开店-选址阶段',
        self::TYPE_OPENSHOP_PREPARATION   => '开店-筹备阶段',
        self::TYPE_OPENSHOP_COLLEGE       => '开店-商学院培训',
        self::TYPE_KFOS_FIRST_ORDER       => '订货-首批订货',
        self::TYPE_OPENSHOP_OPENING       => '开店-开业阶段',
        self::TYPE_KFOS_ORDER             => '订货-日常订货',
        self::TYPE_WAIMAI_OPENING         => '外卖-上线外卖',
        self::TYPE_GROUP_PURCHASE_OPENING => '团购-上线团购',
        self::TYPE_APP_COMPLAINT          => '商家APP-问题反馈',
        self::TYPE_APP_CHOOSE_SUGGESTION  => '商家APP-选品建议',
        self::TYPE_APP_PROCESS_IMPROVE    => '商家APP-流程改进',
        self::TYPE_APP_SHOP_DESIGN        => '商家APP-门店设计',
    ];

    const TYPE_FS_SEND_MAP_TEXT = [
        self::TYPE_WORK_ORDER             => '工单反馈',
        self::TYPE_APP                    => 'App使用反馈',
        self::TYPE_OPENSHOP_SIGN          => '签约反馈',
        self::TYPE_OPENSHOP_SET_LOCATION  => '选址反馈',
        self::TYPE_OPENSHOP_PREPARATION   => '筹备反馈',
        self::TYPE_OPENSHOP_COLLEGE       => '商学院培训反馈',
        self::TYPE_KFOS_FIRST_ORDER       => '订货反馈',
        self::TYPE_OPENSHOP_OPENING       => '开业反馈',
        self::TYPE_KFOS_ORDER             => '订货-日常订货',
        self::TYPE_WAIMAI_OPENING         => '外卖上线反馈',
        self::TYPE_GROUP_PURCHASE_OPENING => '团购上线反馈',
        self::TYPE_APP_COMPLAINT          => '问题反馈',
        self::TYPE_APP_CHOOSE_SUGGESTION  => '选品建议',
        self::TYPE_APP_PROCESS_IMPROVE    => '流程改进',
        self::TYPE_APP_SHOP_DESIGN        => '门店设计',
    ];

    // 不需要关联单号
    const NOT_ORDER_NO_MAPPING = [
        self::TYPE_APP,
        self::TYPE_APP_COMPLAINT,
        self::TYPE_APP_CHOOSE_SUGGESTION,
        self::TYPE_APP_PROCESS_IMPROVE,
        self::TYPE_APP_SHOP_DESIGN
    ];

    //APP-我要反馈 评价列表-merchant
    const APP_APPRAISE_TYPE_LIST = [
        [
            "value" => self::TYPE_APP_SHOP_DESIGN,
            "key"   => self::TYPE_FS_SEND_MAP_TEXT[self::TYPE_APP_SHOP_DESIGN],
            "child" => [],
            "desc"  => "筹备阶段门店空间设计、平面设计问题反馈"
        ],
        [
            "value" => self::TYPE_APP_PROCESS_IMPROVE,
            "key"   => self::TYPE_FS_SEND_MAP_TEXT[self::TYPE_APP_PROCESS_IMPROVE],
            "child" => [],
            "desc"  => "优化流程，提升效率"
        ],
//        [
//            "value" => self::TYPE_APP_CHOOSE_SUGGESTION,
//            "key"   => self::TYPE_FS_SEND_MAP_TEXT[self::TYPE_APP_CHOOSE_SUGGESTION],
//            "child" => [],
//            "desc"  => "感谢您的建议"
//        ],
    ];

    const STATUS_DEFAULT  = 1;
    const STATUS_DEAL_ING = 2;
    const STATUS_DEAL_SUC = 3;

    const STATUS_MAP_TEXT = [
        self::STATUS_DEFAULT  => '待处理',
        self::STATUS_DEAL_ING => '处理中',
        self::STATUS_DEAL_SUC => '完成',
    ];

    const SCORE_ONE   = 1;
    const SCORE_TWO   = 2;
    const SCORE_THREE = 3;
    const SCORE_FOUR  = 4;
    const SCORE_FIVE  = 5;

    const SCORE_MAP_TEXT = [
        self::SCORE_ONE   => '非常不满意',
        self::SCORE_TWO   => '不满意',
        self::SCORE_THREE => '一般',
        self::SCORE_FOUR  => '满意',
        self::SCORE_FIVE  => '非常满意',
    ];

    //5星：蓝色 3-4星：淡黄色 1-2星：红色
    const SCORE_MAP_COLOR = [
        self::SCORE_ONE   => 'red',
        self::SCORE_TWO   => 'red',
        self::SCORE_THREE => 'yellow',
        self::SCORE_FOUR  => 'yellow',
        self::SCORE_FIVE  => 'blue',
    ];


    /**
     * @return HasManyThrough
     */
    public function signOrderRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopSignOrderModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'mainProcessId',
            'shopId',
            'id');
    }

    /**
     * 关联资源表
     * @return HasMany
     */
    public function resourcesRel(): HasMany
    {
        return $this->hasMany(ResourcesModel::class, 'businessId', 'id')
                    ->where('module', ResourcesModel::MODULE_TYPE_APPRAISE)
                    ->where('business', ResourcesModel::BUSINESS_APPRAISE)
                    ->where('name', 'appraiseImg');
    }

    /**
     * 根据id查询详情
     * @param $id
     * @return Builder|Model|object|null
     */
    public static function getInfoById($id)
    {
        return self::query()->where(['id' => $id])->first();
    }

    /**
     * 新增评价
     * @param array $data
     * @return Builder|Model
     */
    public static function addAppraise(array $data)
    {
        return self::query()->create($data);
    }

    /**
     * 修改评价
     * @param $id
     * @param $data
     * @return int
     */
    public static function setInfoById($id, $data): int
    {
        return self::query()->where(['id' => $id])->update($data);
    }

    /**
     * 根据workOrderNo查询详情
     * @param $workOrderNo
     * @param int $type
     * @return Builder|Model|object|null
     */
    public static function getInfoByWorkOrderNo($workOrderNo, $type = 0)
    {
        return self::query()
                   ->where(['workOrderNo' => $workOrderNo])
                   ->when(
                       $type > 0, function ($query) use ($type) {
                       $query->where('type', $type);
                   })
                   ->first();
    }

    /**
     * 根据workOrderNos查询详情
     * @param array $workOrderNos
     * @param int $type
     * @return array
     */
    public static function getInfoByWorkOrderNos(array $workOrderNos, $type = 0): array
    {
        return self::query()
                   ->whereIn('workOrderNo', $workOrderNos)
                   ->when(
                       is_int($type) && $type > 0, function ($query) use ($type) {
                       $query->where('type', $type);
                   })
                   ->when(
                       is_array($type) && count($type), function ($query) use ($type) {
                       $query->whereIn('type', $type);
                   })
                   ->get()
                   ->toArray();
    }

}
